package com.daiqile.mysupertab.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.daiqile.mysupertab.view.NewSwipeRefreshLayout;
import com.daiqile.mysupertab.R;
import com.daiqile.mysupertab.adapter.FunctionsAdapter;
import com.daiqile.mysupertab.base.BaseFragment;
import com.daiqile.mysupertab.model.FunctionList;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends BaseFragment {


    @BindView(R.id.roll_view_pager)
    RollPagerView rollViewPager;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.refreshLayout)
    NewSwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private  FunctionsAdapter adapter;

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void init() {
        //下拉刷新
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
            }
        });
        //轮播图
        rollViewPager.setPlayDelay(3000);
        rollViewPager.setAnimationDurtion(500);
        rollViewPager.setAdapter(new rollNormalAdapter());
        rollViewPager.setHintView(new ColorPointHintView(getActivity(), Color.YELLOW, Color.WHITE));
        //recycleView
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),4,GridLayoutManager.VERTICAL,false);
        recycleView.setLayoutManager(layoutManager);
        adapter = new FunctionsAdapter(getFunctions());
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position==0){
                    Toast.makeText(getActivity(),"只有第一个其他没写，别点了",Toast.LENGTH_SHORT).show();
                }
            }
        });
        recycleView.setAdapter(adapter);




    }
    private List<FunctionList> getFunctions() {
        List<FunctionList> functionsList = new ArrayList<>();
        for (int i = 1;i < 2;i++){
            FunctionList a = new FunctionList("买金",R.drawable.buy);
            functionsList.add(a);
            FunctionList b = new FunctionList("提金",R.drawable.buy);
            functionsList.add(b);
            FunctionList c = new FunctionList("充值",R.drawable.buy);
            functionsList.add(c);
            FunctionList d = new FunctionList("提现",R.drawable.buy);
            functionsList.add(d);
            FunctionList e = new FunctionList("金价走势",R.drawable.buy);
            functionsList.add(e);
            FunctionList f = new FunctionList("签到",R.drawable.buy);
            functionsList.add(f);
            FunctionList g = new FunctionList("银行卡",R.drawable.buy);
            functionsList.add(g);
            FunctionList h = new FunctionList("优惠券",R.drawable.buy);
            functionsList.add(h);
        }
        return functionsList;
    }
    @Override
    public int getFragmentId() {
        return R.layout.fragment_one;
    }

    @Override
    public Object bindFragment() {
        return this;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private class rollNormalAdapter extends StaticPagerAdapter {
        private String[] imgsurl = {
                "http://file2.desktx.com/pc/wallpaper/animals/20090805/Panda_deskTX_1600-1200.jpg",
                "http://soft.tu6.cn/uploads/allimg/120530/1-120530233A10-L.jpg",
                "http://test.bbtang888.com:10061/data/upfiles/litpics/pic1460449090783QQ20160412161751.jpg",
                "http://pic.lvmama.com/uploads/pc/place2/151780/1402654290713.jpg"
        };

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            Glide.with(OneFragment.this)
                    .load(imgsurl[position])
                    .error(R.drawable.timeout)
                    .into(view);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }
        @Override
        public int getCount() {
            return imgsurl.length;
        }
    }
}
